package com.groupten.ServiceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.groupten.Dao.ClassPaperDao;
import com.groupten.Entity.ClassPaperEntity;
import com.groupten.Service.ClassPaperService;
import com.groupten.Vo.ClassVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service("classPaperService")
public class ClassPaperServiceImpl extends ServiceImpl<ClassPaperDao, ClassPaperEntity> implements ClassPaperService {

        @Autowired
        ClassPaperDao classPaperDao;

        @Override
        public Integer insertClassPaper(ClassPaperEntity classPaper) {
                return baseMapper.insert(classPaper);
        }

        @Override
        public Integer deleteByClassName(String className) {
                LambdaQueryWrapper<ClassPaperEntity> queryWrapper = Wrappers.lambdaQuery();
                queryWrapper.eq(ObjectUtils.isNotEmpty(className),ClassPaperEntity::getClassname,className);
                return baseMapper.delete(queryWrapper);
        }

        @Override
        public Integer deleteByPaperSession(Integer paperSessionId) {
                LambdaQueryWrapper<ClassPaperEntity> queryWrapper = Wrappers.lambdaQuery();
                queryWrapper.eq(ObjectUtils.isNotEmpty(paperSessionId),ClassPaperEntity::getPaperSession, paperSessionId);
                return baseMapper.delete(queryWrapper);
        }

        @Override
        public Integer deleteByClassNameAndPaperSession(String className, Integer paperSessionId) {
                LambdaQueryWrapper<ClassPaperEntity> queryWrapper = new LambdaQueryWrapper<ClassPaperEntity>()
                        .eq(className!=null && !className.isEmpty(), ClassPaperEntity::getClassname, className)
                        .eq(paperSessionId!=null && paperSessionId>0, ClassPaperEntity::getPaperSession, paperSessionId);
                return baseMapper.delete(queryWrapper);
        }

        @Override
        public IPage<ClassPaperEntity> selectAll(Integer page, Integer limit) {
                int curPage = 1;
                int curLimit = 10;
                if (ObjectUtils.isNotEmpty(page)) {
                        curPage = Integer.parseInt(page.toString());
                }
                if (ObjectUtils.isNotEmpty(limit)) {
                        curLimit = Integer.parseInt(limit.toString());
                }

                IPage<ClassPaperEntity> pageObject = Page.of(curPage, curLimit);

                return baseMapper.selectPage(pageObject, Wrappers.lambdaQuery());
        }

        @Override
        public IPage<ClassPaperEntity> selectByClassName(String className, Integer page, Integer limit) {
                LambdaQueryWrapper<ClassPaperEntity> queryWrapper = Wrappers.lambdaQuery();
                queryWrapper.eq(ObjectUtils.isNotEmpty(className), ClassPaperEntity::getClassname, className);

                int curPage = 1;
                int curLimit = 10;
                if (ObjectUtils.isNotEmpty(page)) {
                        curPage = Integer.parseInt(page.toString());
                }
                if (ObjectUtils.isNotEmpty(limit)) {
                        curLimit = Integer.parseInt(limit.toString());
                }

                IPage<ClassPaperEntity> pageObject = Page.of(curPage, curLimit);

                return baseMapper.selectPage(pageObject, queryWrapper);
        }

        @Override
        public IPage<ClassPaperEntity> selectByPaperSession(Integer paperSessionID, Integer page, Integer limit) {
                LambdaQueryWrapper<ClassPaperEntity> queryWrapper = Wrappers.lambdaQuery();
                queryWrapper.eq(ObjectUtils.isNotEmpty(paperSessionID), ClassPaperEntity::getPaperSession, paperSessionID);
                queryWrapper.orderByAsc(ClassPaperEntity::getAverageScore);
                
                int curPage = 1;
                int curLimit = 10;
                if (ObjectUtils.isNotEmpty(page)) {
                        curPage = Integer.parseInt(page.toString());
                }
                if (ObjectUtils.isNotEmpty(limit)) {
                        curLimit = Integer.parseInt(limit.toString());
                }
                IPage<ClassPaperEntity> pageObject = Page.of(curPage, curLimit);
                return baseMapper.selectPage(pageObject, queryWrapper);
        }

        @Override
        public ClassPaperEntity selectByClassNameAndPaperSession(String className, Integer paperSessionId) {
                return classPaperDao.selectByClassAndSession(className, paperSessionId);
        }

        @Override
        public Integer updateData(ClassPaperEntity classPaper) {
                return classPaperDao.updateData(classPaper);
        }

        @Override
        public ClassVo getMaxAndMinInClass(ClassVo classVo) {
               List<Double> scores = classPaperDao.selectScoreByClassAndSession(classVo.getClassname(),classVo.getPaperSession());
               classVo.setHighestScore(0.0);
               classVo.setLowestScore(100.0);

               for (Double score : scores) {
                        classVo.setHighestScore(classVo.getHighestScore() > score ? classVo.getHighestScore() : score );
                        classVo.setLowestScore(classVo.getLowestScore() < score ? classVo.getLowestScore() : score );
               }

               return classVo;

        }


}